package com.ywb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywb.entity.EduTeacher;
import com.ywb.entity.vo.TeacherConditionVo;
import com.ywb.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ywb
 * @since 2021-05-27
 */
@Controller
@RequestMapping("/teacher")
public class EduTeacherController extends BaseController{
    @Autowired
    EduTeacherService teacherService;

    /**
     * 路由：教师添加
     * @return
     */
    @GetMapping("/save")
    public String toTeacherSave(){
        request.setAttribute("pageName", "teacher/save.jsp");
        return "index";
    }

    /**
     * 操作：教师添加
     * @param teacher
     * @return
     */
    @PostMapping("/addTeacher")
    public String save( EduTeacher teacher){
        teacherService.save(teacher);

//        url重定向到教师列表界面
        return "redirect:/teacher/list";
    }

    @GetMapping("/list_al")
    public String listAl(Model model){
        List<EduTeacher> list = teacherService.list(null);
        model.addAttribute("list", list);

        model.addAttribute("pageName", "teacher/list_alter_el.jsp");

        return "index";
    }

    /**
     * 路由：教师列表
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(Model model){
        long current = 1, limit = 10;
        Page<EduTeacher> page = new Page<>(current, limit);
        teacherService.page(page, new QueryWrapper<EduTeacher>().orderByDesc("gmt_modified"));
        model.addAttribute("page", page);

        model.addAttribute("total", page.getTotal() / page.getSize() + 1);
        model.addAttribute("pageName", "teacher/list.jsp");

        return "index";
    }

    /**
     * 教师列表带分页
     * @param model
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/list/{current}/{limit}")
//    使用SpringBoot中的Model对象来存放信息
    public String findAll(Model model, @PathVariable(required = false) long current,
                          @PathVariable(required = false)long limit){

        Page<EduTeacher> page = new Page<>(current, limit);
        teacherService.page(page, new QueryWrapper<EduTeacher>().orderByDesc("gmt_modified"));
        model.addAttribute("page", page);

        model.addAttribute("total", page.getTotal() / page.getSize() + 1);
        model.addAttribute("pageName", "teacher/list.jsp");

        return "index";
    }


    /**
     * 条件分页查询
     * @param model
     * @param teacherCondition
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/conditionPage/{current}/{limit}")
    public String conditionPage(Model model,
                                TeacherConditionVo teacherCondition,
                                @PathVariable long current,
                                @PathVariable long limit){
        Page<EduTeacher> page = teacherService.getByTeacherCondition(teacherCondition, current, limit);

        model.addAttribute("page", page);
        model.addAttribute("total", page.getTotal() / page.getSize() + 1);

        model.addAttribute("pageName", "teacher/list.jsp");
        return "index";
    }

    //TODO条件分页查询
    @GetMapping("/conditionPage")
    public String conditionPage(Model model,
                                TeacherConditionVo teacherCondition){
        Page<EduTeacher> page = teacherService.getByTeacherCondition(teacherCondition, 1, 999);

        model.addAttribute("page", page);
        model.addAttribute("total", page.getTotal() / page.getSize() + 1);

        model.addAttribute("pageName", "teacher/list.jsp");
        return "index";
    }



    /**
     * 查找教师 ： 用于更新
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/findById/{id}")
    public String findById(@PathVariable String id, Model model){
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>();
        wrapper.eq("id", id);
        EduTeacher teacher = teacherService.getOne(wrapper);
        model.addAttribute("teacher", teacher);
        model.addAttribute("pageName", "teacher/update.jsp");
        return "index";
    }

    /**
     * 更新教师信息
     * @return
     */
    @PostMapping("/updateTeacher")
    public String updateTeacher(EduTeacher teacher){
        teacherService.updateById(teacher);
        return "redirect:/teacher/list";
    }

    /**
     * 操作：删除教师
     * @param id
     * @return
     */
    @RequestMapping("/deleteTeacher/{id}")
    public String deleteTeacher(@PathVariable String id){
        teacherService.removeById(id);
        return "redirect:/teacher/list";
    }

    @RequestMapping("/deleteBatchIds")
    public void deleteBatchIds(String teacherIds){
        String[] split = teacherIds.split(",");
        teacherService.removeByIds(Arrays.asList(split));
    }




}

